package org.xukai.core.aop.asm;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author xukai
 * @desc
 * @date 2016-11-14-下午 6:48
 */
public class MyClassLoader extends ClassLoader{

    public MyClassLoader(ClassLoader parent) {

        super(parent);

    }

    public Class loadClass(String name) throws ClassNotFoundException {

        if(!"reflection.MyObject".equals(name))

        return super.loadClass(name);

        try {
            String url = "file:C:/data/projects/tutorials/web/WEB-INF/" +
            "classes/reflection/MyObject.class";
            URL myUrl = new URL(url);
            URLConnection connection = myUrl.openConnection();
            InputStream input = connection.getInputStream();
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            int data = input.read();
            while(data != -1){
                buffer.write(data);
                data = input.read();
            }
            input.close();
            byte[] classData = buffer.toByteArray();
            return defineClass("reflection.MyObject", classData, 0, classData.length);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws
    ClassNotFoundException,
    IllegalAccessException,
    InstantiationException {
        ClassLoader parentClassLoader = MyClassLoader.class.getClassLoader();
        MyClassLoader classLoader = new MyClassLoader(parentClassLoader);
        Class myObjectClass = classLoader.loadClass("reflection.MyObject");
         Object   object1 = myObjectClass.newInstance();
       Object object2 = myObjectClass.newInstance();
        //create new class loader so classes can be reloaded.
        classLoader = new MyClassLoader(parentClassLoader);
        myObjectClass = classLoader.loadClass("reflection.MyObject");
        object1 =  myObjectClass.newInstance();
        object2 =  myObjectClass.newInstance();
    }

//    public class MyObject extends MyObjectSuperClass implements AnInterface2{
//                //... body of class ... override superclass methods
//                //    or implement interface methods
//    }


}
